//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.neurosky.thinkgear;

import android.util.Log;
import com.neurosky.thinkgear.DistanceArray;
import com.neurosky.thinkgear.EkgEpoch;
import com.neurosky.thinkgear.EkgParameters;
import com.neurosky.thinkgear.EkgTemplate;
import com.neurosky.thinkgear.TGDevice;

public class EkgSense {
    private final boolean a;
    public EkgParameters params;
    public EkgTemplate[] templates;
    public EkgTemplate currentData;
    public int lastTemplateInd;
    public float lastEpochValue;

    public EkgSense(EkgParameters var1) {
        this.a = TGDevice.ekgPersonalizationEnabled;
        this.lastTemplateInd = 0;
        this.lastEpochValue = 0.0F;
        this.params = var1;
        this.templates = new EkgTemplate[40];
        this.currentData = new EkgTemplate();
    }

    public void reset() {
        this.templates = new EkgTemplate[40];
        this.currentData = new EkgTemplate();
        this.lastTemplateInd = 0;
    }

    public void addTemplate(String var1, float[][] var2) {
        this.templates[this.lastTemplateInd] = new EkgTemplate(var1, var2);
        ++this.lastTemplateInd;
    }

    public void addTemplate(EkgTemplate var1) {
        this.templates[this.lastTemplateInd] = var1;
        Log.v("EkgSense", "Loaded template: " + var1.subjectName + " at index: " + this.lastTemplateInd);
        ++this.lastTemplateInd;
    }

    public String getClassificationResults() {
        if(!this.a) {
            return "Unknown";
        } else {
            int var1 = 0;
            float var2 = 1000000.0F;

            for(int var3 = 0; var3 < this.lastTemplateInd; ++var3) {
                if(this.templates[var3].score < var2) {
                    var1 = var3;
                    var2 = this.templates[var3].score;
                }
            }

            float var5 = 1000000.0F;

            for(int var4 = 0; var4 < this.lastTemplateInd; ++var4) {
                if(this.templates[var4].score < var5 & this.templates[var4].score != var2) {
                    var5 = this.templates[var4].score;
                }
            }

            if(var2 < (float)this.params.templateMaxDistance) {
                return this.templates[var1].subjectName;
            } else {
                return "Unknown";
            }
        }
    }

    public boolean processData(float[] var1) {
        System.nanoTime();
        if(!this.a) {
            return false;
        } else {
            int var2 = this.params.epochLen / 4;
            EkgEpoch var8;
            EkgEpoch var3 = (var8 = new EkgEpoch(var1)).subEpoch(var2, this.params.epochLen - var2);
            int[] var4;
            (var4 = new int[2])[0] = this.params.prePeakLatency;
            var4[1] = this.params.postPeakLatency;
            float var11;
            int var14;
            if((var14 = var3.find_heart_beats(var4, (float)this.params.prePeakAmplitude)) != -1 && (var11 = var3.data[var14]) != this.lastEpochValue) {
                this.lastEpochValue = var11;
                if((var8 = var8.subEpoch(var14, var14 + 2 * var2)).detectHighTail(var14)) {
                    var8 = null;
                }

                if(var8 == null) {
                    if(this.params.verboseMatlab == 1) {
                        System.out.println("Epoch removed");
                    }
                } else if(this.params.verboseMatlab == 1) {
                    System.out.println("Epoch selected");
                }

                if(var8 != null && var8.getLineNoiseAmplitude() < (float)this.params.lineNoiseThreshold && (var8 = (var8 = var8.smooth(this.params.smoothing).detrend()).subtract(var8.median())).std() < (float)this.params.artifactStdThreshold) {
                    if(this.params.stanley == 1) {
                        var8 = (var8 = var8.subEpoch2(57, 67, 81, 97)).subtract(var8.median());
                    }

                    this.currentData.addData(var8);
                    if(this.currentData.lastTemplateIndex > this.params.templateNum) {
                        DistanceArray var13;
                        float[] var9 = new float[(var13 = this.currentData.computeDistance(this.currentData)).numRows];
                        int[][] var12 = new int[var13.numRows][this.params.templatesForDist];

                        int var7;
                        for(var14 = 0; var14 < var13.numRows; ++var14) {
                            EkgEpoch var5;
                            EkgEpoch var6 = (var5 = new EkgEpoch(var13.array[var14])).sort();
                            int[] var16 = var5.sortIndices(var6);
                            var9[var14] = var6.subEpoch(0, this.params.templatesForDist).sum();

                            for(var7 = 0; var7 < this.params.templatesForDist; ++var7) {
                                var12[var14][var7] = var16[var7];
                            }
                        }

                        for(var14 = 0; var14 < var13.numRows; ++var14) {
                            if(this.params.verboseMatlab == 1) {
                                System.out.println("Sum correlation " + var9[var14]);
                            }
                        }

                        var14 = 0;
                        float var18 = var9[0];

                        for(int var19 = 1; var19 < var13.numRows; ++var19) {
                            if(var9[var19] < var18) {
                                var14 = var19;
                                var18 = var9[var19];
                            }
                        }

                        if(var18 < (float)this.params.epochValidMeanThreshold) {
                            float var21 = 0.0F;

                            float var22;
                            for(int var20 = 0; var20 < this.params.templateNum; ++var20) {
                                if((var22 = this.currentData.templateArray[var12[var14][var20]].max()) > var21) {
                                    var21 = var22;
                                }
                            }

                            var18 = (var21 - (float)this.params.epochValidMaxModifier1) / (float)this.params.epochValidMaxModifier2;

                            for(var7 = 0; var7 < this.params.templatesForDist; ++var7) {
                                if(this.params.verboseMatlab == 1) {
                                    System.out.println("Corr selected " + var13.array[var14][var12[var14][var7]]);
                                }
                            }

                            var22 = var13.array[var14][var12[var14][this.params.templatesForDist - 1]];
                            if(this.params.templatesForDist == 3) {
                                var22 = Math.max(var22, var13.array[var12[var14][1]][var12[var14][2]]);
                            }

                            float var15 = (float)this.params.epochValidMaxThreshold + var18;
                            if(this.params.verboseMatlab == 1) {
                                System.out.println("Max corr: " + var22 + ", threshold " + var15);
                            }

                            if(var22 < (float)this.params.epochValidMaxThreshold + var18) {
                                this.currentData.selectRows(var12[var14]);
                                this.currentData.computeDistance(this.currentData);

                                for(int var17 = 0; var17 < this.lastTemplateInd; ++var17) {
                                    DistanceArray var10 = this.currentData.computeDistance(this.templates[var17]);
                                    this.templates[var17].score = var10.mean();
                                }

                                System.nanoTime();
                                return true;
                            }
                        }
                    }
                }
            }

            System.nanoTime();
            return false;
        }
    }
}
